<?php
// 连接数据库
$mysql = new mysqli("localhost", "root", "", "yihe");
if ($mysql -> connect_error) {
	exit("数据库链接失败,原因是:{$mysql -> connect_error}");
}
$mysql -> set_charset("utf8");
$flag = $_GET["flag"];
switch ($flag) {
	case 'select' : {
		select();
		break;
	}
	case 'insert' : {
		insert();
		break;
	}
	case 'update' : {
		update();
		break;
	}
	case 'delete' : {
		delete();
		break;
	}
	case 'set_default' :{
		set_default();
	}
	default :
		break;
}
//<------------------------- 添加地址-------------------------->
function insert() {
	global $mysql;
	$expre_name = $_GET["expre_name"];
	$expre_district = $_GET["expre_district"];
	$expre_detail = $_GET["expre_detail"];
	$expre_tel = $_GET["expre_tel"];
	$fixed_tel = $_GET["fixed_tel"];
	$set_default = $_GET["set_default"];
	//<------------------修改默认地址-------------------->
	if($set_default == 1){
		$set_sql = "update express_info set set_default = 0 where set_default = 1";
		$result = $mysql -> query($set_sql);
	}
	//设置默认地址
	$insert_sql = "insert into express_info (expre_name, expre_district, expre_detail, expre_tel, fixed_tel, set_default) values ('{$expre_name}', '{$expre_district}', '{$expre_detail}', '{$expre_tel}', '{$fixed_tel}', {$set_default})";
	$result = $mysql -> query($insert_sql);
	if ($result) {
		echo "1";
	} else {
		echo "0";
	}
	$mysql -> close();
}
//<------------------------- 查询地址-------------------------->
function select(){
	global $mysql;
	$select_sql = "select * from express_info";
	$result = $mysql -> query($select_sql);
	if($result){
		//创建一个数组
		$expre_arr = array();
		while ($row = $result -> fetch_object()) {
			array_push($expre_arr, $row);
		}
		$json_expre = json_encode($expre_arr);
		echo $json_expre;
	}
	$mysql -> close();
}
//<------------------------- 更新地址-------------------------->
function update(){
	global $mysql;
	$expre_name = $_GET["expre_name"];
	$expre_district = $_GET["expre_district"];
	$expre_detail = $_GET["expre_detail"];
	$orig_detail = $_GET["orig_detail"];
	$expre_tel = $_GET["expre_tel"];
	$fixed_tel = $_GET["fixed_tel"];
	$set_default = $_GET["set_default"];
	//<------------------修改默认地址-------------------->
	if($set_default == 1){
		$set_sql = "update express_info set set_default = 0 where set_default = 1";
		$result = $mysql -> query($set_sql);
	}
	if($fixed_tel == "-"){
		$update_sql = "update express_info set expre_name = '{$expre_name}', expre_district = '{$expre_district}', expre_detail = '{$expre_detail}', expre_tel = '{$expre_tel}', set_default = {$set_default} where expre_detail = '{$orig_detail}'";
	}else{
		$update_sql = "update express_info set expre_name = '{$expre_name}', expre_district = '{$expre_district}', expre_detail = '{$expre_detail}', expre_tel = '{$expre_tel}', fixed_tel = '{$fixed_tel}',  set_default = {$set_default} where expre_detail = '{$orig_detail}'";
	}
	$result = $mysql -> query($update_sql);
	if ($result) {
		echo "1";
	} else {
		echo "0";
	}
	$mysql -> close();
}
//<------------------------- 删除地址-------------------------->
function delete(){
	global $mysql;
	$expre_detail = $_GET["expre_detail"];
	$delete_sql = "delete from express_info where expre_detail = '{$expre_detail}'";
	$result = $mysql -> query($delete_sql);
	if ($result) {
		echo "1";
	} else {
		echo "0";
	}
	$mysql -> close();
}
//<------------------------- 修改默认-------------------------->
function set_default(){
	global $mysql;
	$expre_detail = $_GET["expre_detail"];
	$set_sql = "update express_info set set_default = 0 where set_default = 1";
	$result = $mysql -> query($set_sql);
	$set_sql = "update express_info set set_default = 1 where expre_detail = '{$expre_detail}'";
	$result = $mysql -> query($set_sql);
	if ($result) {
		echo "1";
	} else {
		echo "0";
	}
	$mysql -> close();
}
?>